package com.gupao.normal.user.userserviceprovider.service;

import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.gupao.normal.example.ICouponService;
import org.gupao.normal.user.IUserService;
import org.gupao.normal.user.Person;
import org.springframework.beans.factory.annotation.Autowired;

import javax.validation.ValidationException;
import java.util.concurrent.CompletableFuture;

/**
 * @Author: Mic
 * @公众号： 跟着Mic学架构
 * @官网： https://ke.gupaoedu.cn
 * @Org: 咕泡教育
 **/

@DubboService(cluster = "failfast",timeout = 10000,protocol = "dubbo")
public class UserService implements IUserService {

    @DubboReference(registry = "nacos-registry",version = "2.0") //<dubbo:reference>
    ICouponService couponService;

    //这种方式使用的是UserService的线程
    @Override
    public String selectValidTemplates() {
        //TODO  ,
        // 1. 查询当前用户已经领取过的优惠券
        // 2. 查询优惠券服务，获取当前可以领取的有效优惠券列表
        try {
            Thread.sleep(6000);
            String list = couponService.selectValidCoupons();
            return "用户服务返回：可以领取的优惠券列表，加载到的可领取优惠券："+list;
        }catch (ValidationException e){
            e.printStackTrace();
            return "请求参数校验失败";
        } catch (InterruptedException e) {
            e.printStackTrace();
            return "中断异常";
        }
    }

    @Override
    public String savePerson(Person person) {
        System.out.println("收到的请求数据："+person);
        return "保存用户成功";
    }
}
